package com.linran.javaweb.controller;

import com.linran.javaweb.util.Converter;
import com.linran.javaweb.vo.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("user")
@ConditionalOnBean(value = Converter.class)
public class ValidationController {

    @PostMapping("/validate2")
    public User validate2(@Valid @RequestBody User user) {
        return user;
    }

    /**
     * 该注解@DateTimeFormat对body，form内容都不起作用，只对get参数起作用
     *
     * @param date
     * @return
     */
    @GetMapping("/datetimeformate")
    public Object dateTimeFormateAnnotation(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}
